﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    [ScriptType("SP.RoleAssignmentCollection", ServerTypeId="{2690207a-e174-4d49-b2ca-cff663225dc1}")]
    public class RoleAssignmentCollection : ClientObjectCollection<RoleAssignment>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public RoleAssignmentCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public RoleAssignment Add(Principal principal, RoleDefinitionBindingCollection roleBindings)
        {
            if (base.Context.ValidateOnClient)
            {
                if (principal == null)
                {
                    throw ClientUtility.CreateArgumentNullException("principal");
                }
                if (roleBindings == null)
                {
                    throw ClientUtility.CreateArgumentNullException("roleBindings");
                }
            }
            RoleAssignment assignment = new RoleAssignment(base.Context, new ObjectPathMethod(base.Context, base.Path, "Add", new object[] { principal, roleBindings }));
            assignment.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(assignment.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, assignment);
            base.Context.AddQuery(query);
            base.AddChild(assignment);
            return assignment;
        }

        [Remote]
        public RoleAssignment GetByPrincipal(Principal principalToFind)
        {
            if (base.Context.ValidateOnClient && (principalToFind == null))
            {
                throw ClientUtility.CreateArgumentNullException("principalToFind");
            }
            RoleAssignment assignment = new RoleAssignment(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByPrincipal", new object[] { principalToFind }));
            assignment.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(assignment.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, assignment);
            base.Context.AddQuery(query);
            return assignment;
        }

        [Remote]
        public RoleAssignment GetByPrincipalId(int principalId)
        {
            Dictionary<int, RoleAssignment> dictionary;
            object obj2;
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetByPrincipalId", out obj2))
            {
                dictionary = (Dictionary<int, RoleAssignment>) obj2;
            }
            else
            {
                dictionary = new Dictionary<int, RoleAssignment>();
                base.ObjectData.MethodReturnObjects["GetByPrincipalId"] = dictionary;
            }
            RoleAssignment assignment = null;
            if (!dictionary.TryGetValue(principalId, out assignment))
            {
                assignment = new RoleAssignment(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByPrincipalId", new object[] { principalId }));
                dictionary[principalId] = assignment;
                ObjectIdentityQuery query = new ObjectIdentityQuery(assignment.Path);
                base.Context.AddQueryIdAndResultObject(query.Id, assignment);
                base.Context.AddQuery(query);
            }
            return assignment;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }
    }
}

